import 'dart:async';

import 'package:github/server.dart';

import 'github_repo.dart';

class UserRepo with Repo {
  static UserRepo _instance = UserRepo._();

  UserRepo._();

  factory UserRepo() {
    return _instance;
  }

  CurrentUser _currentUser;

  set currentUser(CurrentUser currentUser) {
    _currentUser = currentUser;
    _userCtl.add(currentUser);
  }

  CurrentUser get currentUser => _currentUser;

  bool get isLogin => _currentUser != null;

  void logout() {
    currentUser = null;
    _userCtl.add(null);
  }

  static StreamController<CurrentUser> _userCtl = StreamController();

  Stream<CurrentUser> currentUserStream = _userCtl.stream.asBroadcastStream();

  void refreshUserInfo() async {
    this.currentUser = await client.users.getCurrentUser();
  }
}
